package ClientLibrary.UI;

import ClientLibrary.Board;
import ClientLibrary.Position;
import CommonLibrary.CheckersPosition;
import CommonLibrary.Commnads.MoveRequest;
import CommonLibrary.Point;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;

public class CheckersButton extends JButton {

	private static final long serialVersionUID = 1;
	private int x, y, type;
	private int playerMotionVariant = -1;
    private MainForm form;
    private boolean clicked;
    
    public boolean isClicked()
    {
        return this.clicked;
    }
    
    public void setType(int type)
    {
        this.type = type;
    }
    
    public int getType()
    {
        return this.type;
    }
	
	public CheckersButton(MainForm mainForm, int x, int y) {
		this.x = x;
		this.y = y;
		this.setFocusable(false);
		this.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				buttonPressed();
			}
		});
        this.form = mainForm;
	}
	
	private void buttonPressed() {

        this.clicked = true;
        this.form.updateCheckers(Board.getBoard().getPositions());
        if(!Board.getBoard().isHereEnemy(this.x,  this.y))
        {
            if(this.type != 0 && this.type != 22)
            {
                // Empty cell
                this.form.setBackgroundForButtons(Board.getBoard().findMove(this.x, this.y, true));
            }
            else if(this.type == 22)
            {
                Board.getBoard().setFinish(this.x, this.y);
                // TODO
                ArrayList<Point> step = new ArrayList<Point>();
                step.add(Board.getBoard().getStart().getPoint());
                step.add(Board.getBoard().getFinish().getPoint());
                Board.getBoard().move(step);

                MoveRequest request = new MoveRequest();
                request.setGameId(Board.getBoard().getGameId());
                request.setPlayerId(Board.getBoard().getPlayerId());
                request.setStep(step);
                Board.getBoard().sendRequest(request);
            }
        }

        this.clicked = false;
	}

	public void set(int type) {
		this.type = type;
		switch (type) {
			case CheckersPosition.emptyCell:
				this.setBackground(Color.yellow); break;
			case CheckersPosition.whiteChecker:
				this.setBackground(Color.white); break;
			case CheckersPosition.blackChecker:
				this.setBackground(Color.black); break;
			case CheckersPosition.whiteKing:
				this.setBackground(Color.blue); break;
			case CheckersPosition.blackKing:
				this.setBackground(Color.green); break;
		}
	}
    
    public int getSide()
    {
        if(this.type == 1 ||this.type == 3)
        {
            return 1;
        }
        else if(this.type == 2 ||this.type == 4)
        {
            return 0;
        }
        else
        {
            return -1;
        }
    }
	
	public void setActivity(int v) {
		this.playerMotionVariant = v;
		if (v >= 0) {
			this.setBackground(new Color(100, 200, 100));
		} else set(type);
	}

}
